/*
Name:Child_Class
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
	public partial class Child_Class:IChild_Class
	{
		private DbSqlServer helper;
		
		#region constructors
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public Child_Class()
        {
			helper = new DbSqlServer(ConnectionString.SqlServer_GPriceWeb);
        }
		
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
		public Child_Class(string connString)
		{
			helper = new DbSqlServer(connString);	
		}
		#endregion
		
		#region IDataBase<Child_Class> 

        public int Add(Child_ClassEntity entity)
        {
           	StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO Child_Class(");
			strSql.Append("PClass_ID,CClass_Name,CClass_Table,CClass_Comment,CClass_Format,CClass_Order)");
			strSql.Append(" VALUES (");
			strSql.Append("@PClass_ID,@CClass_Name,@CClass_Table,@CClass_Comment,@CClass_Format,@CClass_Order)");
			SqlParameter[] parameters = {
					new SqlParameter("@PClass_ID",SqlDbType.UniqueIdentifier),
					new SqlParameter("@CClass_Name",SqlDbType.VarChar),
					new SqlParameter("@CClass_Table",SqlDbType.VarChar),
					new SqlParameter("@CClass_Comment",SqlDbType.VarChar),
					new SqlParameter("@CClass_Format",SqlDbType.Text),
					new SqlParameter("@CClass_Order",SqlDbType.Int)
					};
				parameters[0].Value = entity.PClass_ID;
				parameters[1].Value = entity.CClass_Name;
				parameters[2].Value = entity.CClass_Table;
				parameters[3].Value = entity.CClass_Comment;
				parameters[4].Value = entity.CClass_Format;
				parameters[5].Value = entity.CClass_Order;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
        public int Update(Child_ClassEntity entity)
        {
			
           StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE Child_Class SET ");
				strSql.Append("PClass_ID=@PClass_ID,");
				strSql.Append("CClass_Name=@CClass_Name,");
				strSql.Append("CClass_Table=@CClass_Table,");
				strSql.Append("CClass_Comment=@CClass_Comment,");
				strSql.Append("CClass_Format=@CClass_Format,");
				strSql.Append("CClass_Order=@CClass_Order");
		
			strSql.Append(" WHERE CClass_ID=@CClass_ID");
			SqlParameter[] parameters = {
					new SqlParameter("@CClass_ID",SqlDbType.UniqueIdentifier),
					new SqlParameter("@PClass_ID",SqlDbType.UniqueIdentifier),
					new SqlParameter("@CClass_Name",SqlDbType.VarChar),
					new SqlParameter("@CClass_Table",SqlDbType.VarChar),
					new SqlParameter("@CClass_Comment",SqlDbType.VarChar),
					new SqlParameter("@CClass_Format",SqlDbType.Text),
					new SqlParameter("@CClass_Order",SqlDbType.Int)
					};
				parameters[0].Value = entity.CClass_ID;
				parameters[1].Value = entity.PClass_ID;
				parameters[2].Value = entity.CClass_Name;
				parameters[3].Value = entity.CClass_Table;
				parameters[4].Value = entity.CClass_Comment;
				parameters[5].Value = entity.CClass_Format;
				parameters[6].Value = entity.CClass_Order;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
		public int UpdateByCClass_ID(Child_ClassEntity entity)
        { 
            string where=string.Format("CClass_ID='{0}'",entity.CClass_ID);
			entity.CClass_ID=null;
        	return Updatepartial(entity, where);
		}

        public int Delete(string strWhere)
        {
           StringBuilder strSql=new StringBuilder();
			strSql.Append("DELETE FROM Child_Class ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteNonQuery(strSql.ToString());
        }

        public Child_ClassEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select * from Child_Class ");
			strSql.Append(" where CClass_ID=@primaryKeyId");
			SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
			parameters[0].Value = primaryKeyId;
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
				Child_ClassEntity entity=new Child_ClassEntity();
				if(!Convert.IsDBNull(dr["CClass_ID"]))
                {
                    entity.CClass_ID = (Guid)dr["CClass_ID"];
                }
				if(!Convert.IsDBNull(dr["PClass_ID"]))
                {
                    entity.PClass_ID = (Guid)dr["PClass_ID"];
                }
				entity.CClass_Name=dr["CClass_Name"].ToString();		
				entity.CClass_Table=dr["CClass_Table"].ToString();		
				entity.CClass_Comment=dr["CClass_Comment"].ToString();		
				entity.CClass_Format=dr["CClass_Format"].ToString();		
				if(!Convert.IsDBNull(dr["CClass_Order"]))
                {
                    entity.CClass_Order = (int)dr["CClass_Order"];
                }
				dr.Close();
            	dr.Dispose();
				return entity;
			}
			else
			{
				dr.Close();
            	dr.Dispose();
				return null;
			}
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM Child_Class", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM Child_Class Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

		public DataTable GetTable(string strWhere)
		{
           StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT CClass_ID,PClass_ID,CClass_Name,CClass_Table,CClass_Comment,CClass_Format,CClass_Order");
			strSql.Append(" FROM Child_Class ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteAdapter(strSql.ToString());
		}
		
        public SortList<Child_ClassEntity> GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select CClass_ID,PClass_ID,CClass_Name,CClass_Table,CClass_Comment,CClass_Format,CClass_Order");
			strSql.Append(" FROM Child_Class ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
			SortList<Child_ClassEntity> list= new SortList<Child_ClassEntity>();
			while (dr.Read())
            {
				Child_ClassEntity entity=new Child_ClassEntity();
				if(!Convert.IsDBNull(dr["CClass_ID"]))
                {
                    entity.CClass_ID = (Guid)dr["CClass_ID"];
                }
				if(!Convert.IsDBNull(dr["PClass_ID"]))
                {
                    entity.PClass_ID = (Guid)dr["PClass_ID"];
                }
				entity.CClass_Name=dr["CClass_Name"].ToString();
				entity.CClass_Table=dr["CClass_Table"].ToString();
				entity.CClass_Comment=dr["CClass_Comment"].ToString();
				entity.CClass_Format=dr["CClass_Format"].ToString();
				if(!Convert.IsDBNull(dr["CClass_Order"]))
                {
                    entity.CClass_Order = (int)dr["CClass_Order"];
                }
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }
		
				
		#region select,add,update the fields that you want to operate.
		
		public int AddPartial(Child_ClassEntity entity)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "Child_Class", ref sql);

            if (param!=null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
           return reval;            
            
        }
		
		public int Updatepartial(Child_ClassEntity entity, string where)
		{
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "Child_Class",where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
            return reval;     
		}
		
		public List<Child_ClassEntity> GetPartial(string[] selectedFields,string where)
        {
            List<Child_ClassEntity> list = new List<Child_ClassEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "Child_Class", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader= helper.ExecuteReader(sql);
             
                while (reader.Read())
                {
                    Child_ClassEntity model = new Child_ClassEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
		#endregion
		

        #endregion
	}
}
